package com.sinodata.bsm.cicp.finder.oracle;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;

public class OracleListenLogFinder extends Finder {

    public static final Logger logger = Logger.getLogger(OracleListenLogFinder.class);

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String sql = "select dest_name,destination from v$archive_dest";
        String serverName = (String) params.get("SERVERNAME");
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection("oracle", ip, serverName);
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            int i = 0;
            while (rs.next()) {
                String[] logInfo = new String[5];
                list.add(logInfo);
                logInfo[0] = "Archive log " + i;
                logInfo[1] = rs.getString(1);
                logInfo[2] = "Archive log " + rs.getString(1);
                logInfo[3] = rs.getString(1); //文件名称
                logInfo[4] = rs.getString(2); //文件地址
                i++;
            }
        } catch (SQLException e) {
            logger.error("failed to exex sql", e);
        } catch (IOException e) {
            logger.error("failed to reader or write", e);
        } finally {
            if (stat != null) {
                try {
                    stat.close();
                } catch (Exception e) {
                    logger.error("failed to close statement", e);
                }
            }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }
        return list;
    }
}
